local module = {
  _version = "0.1",
  _description = "Texture atlas generation library for LÖVE",
  _url = "https://notabug.org/coz/Lovelas",
  _license = [[
    Copyright (c) 2014 arampl, coz

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
  ]]
}

function module.AtlasCL()
	local po2, info
	for _, v in pairs(arg) do
		if v:sub(1,6) == "--info" then info = true end
		if v:sub(1,5) == "--po2" then po2 = true end
		if v:sub(1,8) == "--silent" then silent = true end
	end
	return Atlas(1, 1, po2 or false, "out", "at1", info, silent)
end

function module.Atlas(w, h, po2, path, file, info, silent)

	local success

	local self = {w = w or 1, h = h or 1, map = {}, uv = {}, po2 = po2 or true}
	self.settings = {}

	local function Node(x, y, w, h)
		return {x = x, y = y, w = w, h = h}
	end

	local function nextp2(n)
		local res = 1
		while res <= n do
			res = res * 2
		end
		return res
	end

	local function add(root, id, w, h)
		if root.left or root.right then
			if root.left then
				local node = add(root.left, id, w, h)
				if node then return node end
			end
			if root.right then
				local node = add(root.right, id, w, h)
				if node then return node end
			end
			return nil
		end

		if w > root.w or h > root.h then return nil end

		local _w, _h = root.w - w, root.h - h

		if _w <= _h then
			root.left = Node(root.x + w, root.y, _w, h)
			root.right = Node(root.x, root.y + h, root.w, _h)
		else
			root.left = Node(root.x, root.y + h, w, _h)
			root.right = Node(root.x + w, root.y, _w, root.h)
		end

		root.w = w
		root.h = h
		root.id = id

		return root
	end

	local function unmap(root)
		if not root then return {} end

		local tree = {}
		if root.id then
			tree[root.id] = {}
			tree[root.id].x, tree[root.id].y = root.x, root.y
            tree[root.id].w, tree[root.id].h = root.w, root.h
		end

		local left = unmap(root.left)
		local right = unmap(root.right)

		for k, v in pairs(left) do
			tree[k] = {}
			tree[k].x, tree[k].y = v.x, v.y
			tree[k].w, tree[k].h = v.w, v.h
		end
		for k, v in pairs(right) do
			tree[k] = {}
			tree[k].x, tree[k].y = v.x, v.y
			tree[k].w, tree[k].h = v.w, v.h
		end

		return tree
	end

	local function getImages(sort)
		local files, images = love.filesystem.getDirectoryItems(path), {}
		for i = 1, #files do
			images[i] = {}
			images[i].name = files[i]
			images[i].image = love.graphics.newImage(path .. "/" .. files[i])
			images[i].w = images[i].image:getWidth()
			images[i].h = images[i].image:getHeight()
			images[i].area = images[i].w * images[i].h
		end
		files = nil
		if sort then table.sort(images, function(a, b) return (a.area > b.area) end) end
		return images
	end

	function self.out(filename, format)
		local format = format or "png"
		local file = love.filesystem.newFile(filename .. ".txt")
		file:open("w")
		for i = 1, #self.uv do
			file:write(self.uv[i].name .. "," .. self.uv[i].x .. "," .. self.uv[i].y .. ";\r\n")
		end
		file:close()
		self.map:getImageData():encode(filename, format)
	end

	function self.build()

		local root = {}
		local w, h = self.w, self.h

		local images = getImages( true )

		if w < images[1].w then w = images[1].w end
		if h < images[1].h then h = images[1].h end

		if po2 then
			if w % 1 == 0 then w = nextp2(w) end
			if h % 1 == 0 then h = nextp2(h) end
		end

		local limits = love.graphics.getSystemLimits()

		repeat
			local node

			root = Node(0, 0, w, h)

			for i = 1, #images do
				node = add(root, i, images[i].w, images[i].h)
				if not node then break end
			end

			if not node then
				if h <= w then
					if po2 then h = h * 2 else h = h + 1 end
				else
					if po2 then w = w * 2 else w = w + 1 end
				end
			else
				break
			end
		until false

		success = (w <= limits.texturesize and h <= limits.texturesize)

		local coords = unmap(root)
		self.map = love.graphics.newCanvas(w, h)
		love.graphics.setCanvas(self.map)
        love.graphics.clear()

		for i = 1, #images do
			love.graphics.draw(images[i].image, coords[i].x, coords[i].y)
			self.uv[i] = {}
			self.uv[i].name = images[i].name
			self.uv[i].x, self.uv[i].y = coords[i].x, coords[i].y
			--self.uv[i].w, self.uv[i].h = coords[i].w, coords[i].h
			self.uv[i].w, self.uv[i].h = images[i].w, images[i].h
			if info then
				print(self.uv[i].name, self.uv[i].x, self.uv[i].y, self.uv[i].w, self.uv[i].h)
			end
		end
		love.graphics.setCanvas()
		images = nil
	end

	if path then self.build() end

	if success then
		if file then self.out(file) end
		if info then print(self.map:getWidth() .. " x " .. self.map:getHeight()) end
		res = self
	else
		res = "toolarge"
	end

	if silent then
		love.event.quit()
	else
		return res
	end
end

return module
